<template>
	<view class="content">
		<u-navbar title="授权登录" @rightClick="rightClick" :placeholder="true" :autoBack="true"></u-navbar>
		<u-avatar src="/static/reCharge/wx.png" size="218rpx" class="avatar"></u-avatar>
		<text class="content_auth">微信授权</text>
		<!-- <text class="content_text">{{title}}</text> -->
		<view class="btn" @click="authorize">点击进行微信授权</view>
	</view>
</template>

<script>
	import wx from 'weixin-js-sdk'

	export default {
		data() {
			return {
				title: '',
				cAllInfoId: ""
			};
		},
		onLoad(options) {
			this.getUserInfo();
		},
		methods: {
			authorize() {
				// 获取公众号的 appid，需要替换为你自己的 appid
				const appid = 'wx0865a9abecd5c81b';
				// 授权后重定向的回调链接，需要替换为你自己的回调链接
				const redirect_uri = encodeURIComponent('https://admin.autobbc.cn/web/#/pages/pcAuthorize/index');
				// 授权类型，固定值
				const response_type = 'code';
				// 应用授权作用域，snsapi_userinfo 表示获取用户信息
				const scope = 'snsapi_userinfo';
				// 重定向到微信授权页面
				const authUrl =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=${response_type}&scope=${scope}&state=${this.cAllInfoId}#wechat_redirect`;
				window.location.href = authUrl;
			},
			getCode() {
				const url = window.location.href;
				const code = url.match(/code=([^&]*)/);
				if (code) {
					return code[1];
				}
				return null;
			},
			getCarShopId() {
				const url = window.location.href;
				const cAllInfoId = url.match(/carShopId=([^&]*)/);
				if (cAllInfoId) {
					// this.cAllInfoId = cAllInfoId[1];
					return cAllInfoId[1];
				}
				return null;
			},
			getState() {
				const url = window.location.href;
				const state = url.match(/state=(\d+)#/);
				if (state) {
					return state[1];
				}
				return null;
			},
			getUserInfo() {
				const code = this.getCode();
				// uni.$u.toast(code)
				const carShopId = this.getCarShopId();
				this.cAllInfoId = carShopId
				const state = this.getState();
				console.log(code);
				console.log(state);
				console.log(carShopId);
				// uni.$u.toast(carShopId);
				let that = this;
				if (code && state) {
					try {
						that.api.pcBindwx({
							// id: uni.getStorageSync("cAllInfoId"),
							id: state,
							code: code
						}).then(res => {
							// uni.$u.toast("授权成功，刷新蜜蜂买卖车后台");
							uni.showModal({
								title: '提示',
								content: '授权成功，返回蜜蜂买卖车后台刷新页面',
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定');
										// 不加setTimeout方法二可能失效
										setTimeout(() => {
											wx.closeWindow()
										}, 1000)
										return
									}
								}
							});
							uni.removeStorageSync("cAllInfoId")
						})
						console.log('用户信息：', userInfoRes.data);
					} catch (error) {
						console.error('获取用户信息失败：', error);
					}
				}
			}
		},

	};
</script>

<style lang="scss" scoped>
	.page {
		margin: 20rpx;
	}


	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 30px;
		box-sizing: border-box;

		.avatar {
			margin-top: 216rpx;
		}

		&_auth {
			font-family: Source Han Sans SC;
			font-weight: bold;
			font-size: 56rpx;
			color: #404040;
			margin-top: 22rpx;
			margin-bottom: 16rpx;
		}

		&_text {
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 28rpx;
			color: #404040;
			margin-bottom: 56rpx;
		}

		.btn {
			width: 100%;
			color: #fff;
			padding: 23rpx 0rpx;
			background-color: #28ac3e;
			text-align: center;
			border-radius: 16rpx;
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #ffffff;
		}
	}
</style>